package com.jiaanpei.frameproject.utils;

import android.app.Activity;
import android.os.Build;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import com.gyf.barlibrary.ImmersionBar;
import com.jiaanpei.frameproject.R;

import java.lang.reflect.Method;

public class StatusBarUtil {
    private static final String KEY_DISPLAY = "ro.build.display.id";

    public static void initStatusBarConfig(Activity activity, Fragment fragment, View titleView) {
        if (ImmersionBar.hasNavigationBar(activity)) {
            addContentPadding(activity, titleView);
//            if (titleView == null) {
//                ImmersionBar.with(activity, fragment).navigationBarEnable(false).init();
//            } else {
//                ImmersionBar.with(activity, fragment).navigationBarEnable(false).titleBar(titleView).init();
//            }
        } else {
            if (titleView == null) {
                ImmersionBar.with(activity, fragment).init();
            } else {
                ImmersionBar.with(activity, fragment).titleBar(titleView).init();
            }
        }
    }

    public static void addContentPadding(Activity activity, View titleView) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            Window window = activity.getWindow();
            window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            if (titleView != null) {
                ViewGroup contentView = window.getDecorView().findViewById(android.R.id.content);
                contentView.setPadding(0, ImmersionBar.getStatusBarHeight(activity), 0, 0);
                contentView.setBackgroundColor(ContextCompat.getColor(activity, R.color.main_color));
            }
        }
    }

    public static void initStatusBarConfig(Activity activity, View titleView) {
        if (ImmersionBar.hasNavigationBar(activity)) {
            addContentPadding(activity, titleView);
//            if (titleView == null) {
//                ImmersionBar.with(activity).navigationBarEnable(false).init();
//            } else {
//                ImmersionBar.with(activity).navigationBarEnable(false).titleBar(titleView).init();
//            }
        } else {
            if (titleView == null) {
                ImmersionBar.with(activity).init();
            } else {
                ImmersionBar.with(activity).titleBar(titleView).init();
            }
        }
    }

    public static void clearStatusBarConfig(Activity activity) {
        ImmersionBar.with(activity).destroy();
    }

    private static String getSystemProperty(String key, String defaultValue) {
        try {
            Class<?> clz = Class.forName("android.os.SystemProperties");
            Method get = clz.getMethod("get", String.class, String.class);
            return (String) get.invoke(clz, key, defaultValue);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return defaultValue;
    }

    public static boolean isColorOSFlag() {
        return Build.MODEL.contains("OPPO R11");//isColorOS().toLowerCase().contains("r11s");
    }

    public static String isColorOS() {
        return getSystemProperty(KEY_DISPLAY, "");
    }

    public static boolean isFuntouchOSFlag() {
        return Build.MODEL.contains("vivo X2")||Build.MODEL.contains("Mate9")||Build.MODEL.contains("HUAWEI VNS-AL00")||Build.BRAND.contains("HUAWEI");//isFuntouchOS().toLowerCase().contains("x20");
    }


    public static String isFuntouchOS() {
        return getSystemProperty(KEY_DISPLAY, "");
    }
}
